<%@page import="drukarnia.model.User"%>
<%@page import="drukarnia.model.Orders"%>
<%@page import="java.util.List"%>
<%@page import="drukarnia.controller.OrderManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page errorPage="WEB-INF/bledy.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Drukarnia</title>
</head>
<body><%
			String message = request.getParameter("message");
			if(message != null){ %>
				<h3 style="color:white; text-align:center; background-color: red">
				
				<% out.println(message);
			}	
		%></h3>
	<%
		User user = (User)session.getAttribute("user");
		if(user != null && user.getAuthorization() == 'a'){
			OrderManager man = new OrderManager();
			List<Orders> ordersList = man.fetchAllOrdersFromDB();
			int perPage = 10;
			int size = ordersList.size()-1;
			int pages;
			int start=0;
			
		    if( request.getParameter("pages") != null && request.getParameter("pages") != "")
		    {
		    	pages = (Integer.parseInt(request.getParameter("pages")));
		    	start = (pages - 1) * perPage;
		    	
		    	if(pages < 1)  pages = 1; 
		    	
		    }
		    else
		    {
		    	pages=1;
		    }
		    
		    int prev = pages - 1;
			int next = pages + 1;
			if(ordersList.isEmpty()){%>
			<center><h3>Brak zamówień</h3>
		<%}else{
	%>   
	<h1>Historia twoich zamówień</h1>
	<center>
		<table style="text-align: left" >
			<tr style="background-color: grey; color:white">
				<th>Nr zamówienia</th>
				<th>Mail</th>
				<th style="width: 100px">Data</th>
				<th>Status</th>
				<th>Szczegóły</th>
				<th>Zmiana statusu: </th>
			</tr>
			<% 
				 for(int i = start; i<start+perPage; i++){
				
					 if(i>size) break;
				    	
				    	
				    	else{ 
				 
			%>
				<tr>
					<td><%= ordersList.get(i).getId_order() %></td>
					<td><%= ordersList.get(i).getId_user().getMail() %></td>
					<td><%= ordersList.get(i).getDate() %></td>
					<td><%= ordersList.get(i).getStatus() %></td>
					<td><a href="orderDetailsADMIN.jsp?id=<%=ordersList.get(i).getId_order() %>" >szczegóły</a></td>
					<td><%
					if(ordersList.get(i).getStatus().equals("Oczekuje na realizacje"))
						out.println("<a href=progressing.jsp?mail="+ordersList.get(i).getId_user().getMail()+"&id_order="+ ordersList.get(i).getId_order() +"> realizuj </a>"); 
					else if(ordersList.get(i).getStatus().equals("w trakcie realizacji"))	
						out.println("<a href=finished.jsp?mail="+ordersList.get(i).getId_user().getMail()+"&id_order="+ ordersList.get(i).getId_order() +"> zakończ </a>");
					else 
						out.println("");
						%></td>
				
			</tr>
				</tr>
			<%	
				}}}
			%>
			<tr>
				
			</tr>
		</table>
		<br><table width=100% border=0 height=1><tr>
		    <%int liczbaStron = (size/perPage)+1; 
		    if(pages > 1){
		    	out.println("<td align=right width=\"45%\"><a href=orderHistoryADMIN.jsp?pages="+prev+">poprzednia strona</a>");
		    }	else {out.println("<td>");}
		    
		    out.println("<td width=\"10%\">strona "+pages+" z "+liczbaStron+"");
		    
		    if(pages <= (size/perPage)){
		    	out.println("<td align=left width=\"45%\"><a href=orderHistoryADMIN.jsp?pages="+next+">następna strona</a>");
		    }	else {out.println("<td>");}
		   
		 	%></table><br>
	</center>
	<h2><a href="index.jsp">Powrót</a></h2>
	 <% }else{
		 response.sendError(404,"Drukarnia: brak uprawnien");
	 	}
		 %>
	
</body>
</html>